﻿using FluentFTP;
using Services.IServices;

namespace Services.Services
{
    public class ftpService : IftpService
    {
        private FtpClient _ftpClient;

        public ftpService(string host, int port, string username, string password)
        {
            _ftpClient = new FtpClient(host, port);
            _ftpClient.Credentials = new System.Net.NetworkCredential(username, password);
        }

        public void Connect()
        {
            try
            {
                _ftpClient.Connect();
                Console.WriteLine("Connected to FTP server successfully.");
            }
            catch (Exception ex)
            {
                throw new Exception("FTP Connect Error: " + ex.Message, ex);
            }
        }

        public void UploadFile(string localFilePath, string remoteFilePath)
        {
            try
            {
                _ftpClient.UploadFile(localFilePath, remoteFilePath);
                Console.WriteLine("File uploaded successfully.");
            }
            catch (Exception ex)
            {
                throw new Exception("FTP Upload Error: " + ex.Message, ex);
            }
        }

        public void DownloadFile(string localFilePath, string remoteFilePath)
        {
            try
            {
                _ftpClient.DownloadFile(localFilePath, remoteFilePath);
                Console.WriteLine("File downloaded successfully.");
            }
            catch (Exception ex)
            {
                throw new Exception("FTP Download Error: " + ex.Message, ex);
            }
        }

        public void Disconnect()
        {
            if (_ftpClient.IsConnected)
            {
                _ftpClient.Disconnect();
                Console.WriteLine("Disconnected from FTP server.");
            }
        }
    }
}
